#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;

const ll N = 1e6 + 10, mod = 1e9 + 7;

ll n, root;
ll a[N], siz[N];
pair<ll, ll> g[N];
ll C[1010][1010];

void insert(ll x, ll k)
{
    if (k < x) {
        if (g[x].first == 0) g[x].first = k;
        else insert(g[x].first, k);
    } else {
        if (g[x].second == 0) g[x].second = k;
        else insert(g[x].second, k);
    }
}

ll dfs(ll x)
{
    if (x == 0) return 1;
    siz[x] = 1;
    ll t1 = dfs(g[x].first);
    siz[x] += siz[g[x].first];
    ll t2 = dfs(g[x].second);
    siz[x] += siz[g[x].second];
    return t1 * t2 % mod * C[siz[g[x].first] + siz[g[x].second]][siz[g[x].first]] % mod;
}

void solve()
{
    while (cin >> n) {
        memset(siz, 0, sizeof siz);
        memset(g, 0, sizeof g);
        for (ll i = 1;i <= n;i++) cin >> a[i];
        root = a[1];
        for (ll i = 2;i <= n;i++) insert(root, a[i]);
        cout << dfs(root) << '\n';
    }
}

signed main()
{
    //ios::sync_with_stdio(0);
    //cin.tie(0);cout.tie(0);
#define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
    std::istringstream in(R"()");
    std::cin.rdbuf(in.rdbuf());
#endif
    ll T = 1;
    //cin>>T;
    C[0][0] = C[1][0] = C[1][1] = 1;
    for (ll i = 1;i <= 1000;i++) {
        C[i][0] = 1;
        for (ll j = 1;j <= i;j++)
            C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod;
    }
    for (ll i = 1;i <= T;i++) {
        solve();
    }
}